package com.cheney.diandireader.server.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 文件操作帮助类
 * @author mayh
 *
 */
public class FileHelper {

	//获得文件的修改时间
	public static String getFileModifyTime(File file){
		String createTime = "";
		if(file!=null){
			long times  = file.lastModified();
			Date date = new Date(times);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			createTime = sdf.format(date);
		}
		return createTime;
	}
	
	//获得编码格式
	public static String getFileEncodeType(File file){
		String encodeName = "gb2312";
		if(file!=null){
			byte buffer[] = new byte[3];
			FileInputStream fis;
			try {
				fis = new FileInputStream(file);
				fis.read(buffer);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		if(buffer[0] == -1 && buffer[1] == -2 )  
            encodeName = "UTF-16";  
        if (buffer[0] == -2 && buffer[1] == -1 )  
            encodeName = "UNICODE";  
        if(buffer[0]==-17 && buffer[1]==-69 && buffer[2] ==-65)  
            encodeName = "UTF-8";  
		}
		return encodeName;
	}
	
	//获得文件后缀
	public static String getFileSuffix(File file){
		String suffix = "";
		if(file!=null){
			if(file.isFile()){
			String fileName = file.getName();
		    suffix  = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()).toLowerCase();
			}else if(file.isDirectory()){
				suffix = "folder";
			}
		}
		return suffix;
	}
	//去掉文件后缀
	public static String getFileWithoutSuffix(String fileName){
		String str = "";
		if(fileName!=null){
			str = fileName.substring(0, fileName.lastIndexOf("."));
		}
		return str;
	}
	
	//获得文件大小
	public static String getFileSize(File file){
		String size = "0B";
		if(file!=null){
			try {
				//格式化 显示样式
				DecimalFormat format = new DecimalFormat("0.00");
				double length = getFileLength(file);
				double tempSize ;
				tempSize = length/1024/1024;
				if(tempSize>=1){
					return size = format.format(tempSize)+"M";
				}
				tempSize = length/1024;
				if(tempSize>=1){
					return size = format.format(tempSize)+"K";
				}
				tempSize = length;
				if(tempSize>=1){
					return size = format.format(tempSize)+"B";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return size;
	}
//	获得文件长度
	public static int getFileLength(File file){
		if(file==null)
			return 0;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			//file.length is only worked in txt
			return fis.available();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	//删除文件
	public static boolean deleteFile(File file){
		boolean flag = true;
		if(file!=null&&file.exists()){
			try{
				file.delete();
			}catch(Exception e){
				flag = false;
			}
		}
		return flag;
	}
	//copy 文件(将输入流写到指定文件)
	public static boolean copyFile(InputStream inputStream,File file){
		if(inputStream==null||file==null)
			return false;
		FileOutputStream fos = null;
		try {
			if(!file.exists()){
				if(!file.createNewFile())return false;
			}
			fos = new FileOutputStream(file);
			byte[] buffer = new byte[255];
			int size = 0;
			while((size = inputStream.read(buffer))>0){
				fos.write(buffer, 0, size);
			}
		}catch(Exception exception){
			exception.printStackTrace();
			return false;
		}finally {
			try {
				fos.flush();
				fos.close();
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
}
